package cn.bonoon.kernel.query;

public class PageRequest implements Pageable {

	private int page;
	private int size;

	public PageRequest(final int page, final int size) {
		if (0 > page) {
			throw new IllegalArgumentException("页码不能小于0");
		}

		if (0 > size) {
			throw new IllegalArgumentException("页面的记录数必须大于0");
		}

		this.page = page;
		this.size = size;
	}

	@Override
	public int getPageNumber() {
		return page;
	}

	@Override
	public int getPageSize() {
		return size;
	}

	@Override
	public int getFirstItem() {
		int i = page - 1;
		i = Math.max(0, i);
		return (i) * size;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}

		if (!(obj instanceof PageRequest)) {
			return false;
		}

		PageRequest that = (PageRequest) obj;

		boolean pageEqual = this.page == that.page;
		boolean sizeEqual = this.size == that.size;

		return pageEqual && sizeEqual;// && sortEqual;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {

		int result = 17;

		result = 31 * result + page;
		result = 31 * result + size;

		return result;
	}
}
